package com.jlu155.CourseManager.service;

import com.jlu155.CourseManager.Bean.PageRequest;
import com.jlu155.CourseManager.Bean.PageResult;
import com.jlu155.CourseManager.entity.Course;
import com.jlu155.CourseManager.entity.Session;
import com.jlu155.CourseManager.entity.StudentCourse;
import com.jlu155.CourseManager.entity.TeachClass;
import com.jlu155.CourseManager.repository.CourseRepository;
import com.jlu155.CourseManager.repository.StudentCourseRepository;
import com.jlu155.CourseManager.repository.StudentRepository;
import com.jlu155.CourseManager.repository.TeachClassRepository;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentCourseService {
    @Autowired
    private StudentCourseRepository studentCourseRepository;
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private TeachClassService teachClassService;
    @Autowired
    private TeachClassRepository teachClassRepository;

    @Autowired
    private CourseRepository courseRepository;


    public void fillViewInfo(StudentCourse studentCourse, boolean withTeachClass, boolean withSessions) {
        studentCourse.setStudent(studentRepository.findStudentById(studentCourse.getStudent_id()));
        if (withTeachClass) {
            studentCourse.setTeachClass(teachClassRepository.findTeachClassById(studentCourse.getTc_id()));
            teachClassService.associateMult(studentCourse.getTeachClass(), withSessions, false);
        }
    }

    public String chooseCourse(Integer student_id, Integer tc_id) {
        TeachClass teachClass = teachClassRepository.findTeachClassById(tc_id);
        if (teachClass.getSelected() >= teachClass.getCapacity()) return "人数已满";
        List<StudentCourse> studentCourseList = studentCourseRepository.findStudentCourseByStudentId(student_id);
        for (StudentCourse sc : studentCourseList) {
            if (sc.getTc_id() == tc_id) return "重复选课";
            sc.setTeachClass(teachClassRepository.findTeachClassById(sc.getTc_id()));
        }


        //备选课程与已选课程时间冲突检测
        Integer start_week = teachClass.getStart_week();
        Integer end_week = teachClass.getEnd_week();
        //try {
            teachClassService.associateMult(teachClass,true,false);
            List<Session> sessionList= teachClass.getSessionList();
            if(sessionList!=null){
            for (Session session : sessionList) {
                Integer weekday = session.getWeekday();
                Integer start_time = session.getStart_time();
                Integer end_time = session.getEnd_time();
                for (StudentCourse studentCourse : studentCourseList) {
                    Integer sc_start_week = studentCourse.getTeachClass().getStart_week();
                    Integer sc_end_week = studentCourse.getTeachClass().getEnd_week();
                    //try {
                        if ((start_week >= sc_start_week && start_week <= sc_end_week) || (end_week >= sc_start_week && end_week <= sc_end_week) ||
                                (sc_start_week >= start_week && sc_start_week <= end_week) || (sc_start_week >= start_week && sc_end_week <= end_week)) {
                            TeachClass sc_teachClass=studentCourse.getTeachClass();
                            teachClassService.associateMult(sc_teachClass,true,false);
                            List<Session> sc_sessionList=sc_teachClass.getSessionList();
                            if(sc_sessionList==null)break;
                            for (Session sc_session : sc_sessionList) {
                                Integer sc_weekday = sc_session.getWeekday();
                                Integer sc_start_time = sc_session.getStart_time();
                                Integer sc_end_time = sc_session.getEnd_time();
                                if (sc_weekday == weekday) {
                                    if ((start_time >= sc_start_time && start_time <= sc_end_time) || (end_time >= sc_start_time && end_time <= sc_end_time) ||
                                            (sc_start_time >= start_time && sc_start_time <= end_time) || (sc_start_time >= start_time && sc_end_time <= end_time))
                                        return "与"+sc_teachClass.getCourse().getName()+"课程时间冲突";
                                }
                            }
                        }
                    }// catch (NullPointerException e) {
                       // ;
                   // }
                }
            }
        //} //catch (NullPointerException e) {
            //return -3;
        //}

        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setGrade(-1);
        studentCourse.setCourse_id(teachClass.getCourse_id());
        studentCourse.setStudent_id(student_id);
        studentCourse.setTc_id(tc_id);
        studentCourse.setCourse(courseRepository.findCourseById(teachClass.getCourse_id()));
        studentCourse.setStudent(studentRepository.findStudentById(student_id));
        studentCourse.setTeachClass(teachClass);
        studentCourseRepository.insertStudentCourse(studentCourse);
        return "ok";

    }

    public PageResult<StudentCourse> getSelectedCoursesPage(PageRequest pageRequest) {
        PageResult<StudentCourse> pageResult = new PageResult<StudentCourse>();
        List<StudentCourse> studentCourses;
        studentCourses = studentCourseRepository.findStudentCourseByConditions(pageRequest.getSid(), pageRequest.getTc_id(),
                (pageRequest.getIndex() - 1) * pageRequest.getSize(), pageRequest.getSize());
        int total = studentCourseRepository.findStudentCourseByConditions(pageRequest.getSid(), pageRequest.getTc_id(),
                0,1000000).size();
        pageResult.setPageList(studentCourses);
        pageResult.setPageNumber(total);
        return pageResult;
    }

}
